package com.online.music.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.music.common.Result;
import com.online.music.entity.vo.UserVo;
import com.online.music.utils.UserContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.online.music.service.IUserService;
import com.online.music.entity.po.User;

import org.springframework.web.bind.annotation.RestController;


/**
 * @Author: Leno
 * @Date: 2025-01-31
 * @Description: user接口
 */
@RequiredArgsConstructor
@Slf4j
@Tag(name = "user接口")
@RestController
@RequestMapping("/user")
public class UserController {

    private final IUserService userService;

    @Operation(summary = "查询所有User")
    @GetMapping
    public Result<List<User>> findAll() {
        return Result.success(userService.list());
    }

    @Operation(summary = "根据id查询User")
    @GetMapping("/{id}")
    public Result<User> findOne(@PathVariable Long id) {
        return Result.success(userService.getById(id));
    }

    @Operation(summary = "根据分页查询User")
    @GetMapping("/page")
    public Result<Page<User>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, User user) {
        return userService.findPage(pageNum, pageSize, user);
    }

    @Operation(summary = "添加或修改User")
    @PostMapping
    public Result<Boolean> save(@RequestBody @Valid User user) {
        return Result.success(userService.saveOrUpdate(user));
    }

    @Operation(summary = "根据id批量删除User")
    @DeleteMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Long> ids) {
        return userService.deleteBatch(ids);
    }


    // 登录注册
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<UserVo> login(@RequestBody @Valid User user) {
        return userService.login(user);
    }

    @Operation(summary = "获取当前用户信息")
    @GetMapping("/info")
    public Result<User> getCurrentUser() {
        return Result.success(userService.getById(UserContextUtil.getUserId()));
    }

    @Operation(summary = "注册")
    @PostMapping("/register")
    public Result<String> register(@RequestBody @Valid User user) {
        return userService.register(user);
    }

}
